package dao;

import static common.Constants.EMPTY_ADMIN_LIST;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import common.Constants;

import domain.Admin;
import domain.User;
import domain.UserRelation;

public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {

	@Override
	public List<Admin> findAdminsByLikeName(final String name,final Integer pageSize, final Integer pageNumber) {
		final String HQL = "From Admin a where a.name like :name";
		// List<User> users = getHibernateTemplate().find(HQL,new Object[]{name+"%"});
		// 下面的可以用上面的代替
		@SuppressWarnings("unchecked")
		List<Admin> users = (List<Admin>) getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public List<User> doInHibernate(Session session) throws HibernateException, SQLException {
				if (session != null && session.isOpen()) {
					Query query = session.createQuery(HQL);
					query.setParameter("name", name + "%");
					query.setFirstResult(pageSize * (pageNumber - 1));
					query.setMaxResults(pageSize); 
					List<User> users = query.list();
					return users;
				}
				return null;
			}
		});
		if (users == null || users.isEmpty()) {
			return EMPTY_ADMIN_LIST;
		}
		return users;
	}

	@Override
	public Long countAllAdmins() {
		@SuppressWarnings("unchecked")
		Long totalCount = (Long) getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Long doInHibernate(Session session) throws HibernateException, SQLException {
				if (session != null && session.isOpen()) {
					String hql = "SELECT COUNT(a.id) FROM Admin a";
					Query query = session.createQuery(hql);
					List<Long> list = query.list();
					if (list != null) {
						return list.get(0);
					} else {
						return new Long(0);
					}
				}
				return new Long(0);
			}
		});
		return totalCount;
	}

	@Override
	public Admin addAdmin(final Admin admin) {
		Admin savedAdmin = 
		(Admin) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Admin doInHibernate(Session session) throws HibernateException, SQLException {
				if(session!=null && session.isOpen()) {
					session.save(admin);
					return admin;
				}
				return null;
			}
		});
		return savedAdmin;
	}

	@Override
	public void deleteAdminById(final Integer id) {
		getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public UserRelation doInHibernate(Session session) throws HibernateException, SQLException {
				if (session != null && session.isOpen()) {
					String sql =
						new StringBuilder().append("DELETE FROM ").append(Constants.getDefaultSchema())
							.append(".t_admin ").append(" WHERE ").append(" t_id=:id ").toString();
					SQLQuery query = session.createSQLQuery(sql);
					query.setInteger("id", id);
					query.executeUpdate();
				}
				return null;
			}
		});
	}

	@Override
	public Admin updateAdmin(Admin admin) {
		getHibernateTemplate().saveOrUpdate(admin);
		return admin;
	}

	@Override
	public Admin findAdminById(Integer id) {
		Admin admin = (Admin) getHibernateTemplate().get(Admin.class, id);
		return admin;
	}

}
